home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991 …esperately Seeking Seven / Desperately Seeking Seven.2mg / Dev.CD.8 / Essentials / Tools / DTS.Samples / SC20DTSToolLib / OffScn.Port.asm < prev    next >
Encoding:
Assembly Source File  |  1990-05-25  |  13.8 KB  |  395 lines  |  [04] ASCII Text (0x0000)

  1. *******************************************************************************
  2. *
  3. * off-screen port routines -- Version 3.0
  4. *
  5. * Copyright (c)
  6. * Apple Computer, Inc.  1989-1990
  7. * All Rights Reserved.
  8. *
  9. * Written by Eric Soldan.
  10. *
  11. * Developer Technical Support Apple II Sample Code
  12. *
  13. * The purpose of this code is to make creating and disposing of off-screen
  14. * grafPorts more convenient.
  15. *
  16. *******************************************************************************
  17.                     case on
  18.                     longi on
  19.                     longa on
  20.  
  21.                     copy 2/ainclude/e16.types
  22.  
  23.                     copy 2/ainclude/e16.memory
  24.                     copy 2/ainclude/e16.quickdraw
  25.                     mcopy macros/ofscn.macros
  26.  
  27. ***********************************************************************
  28.  
  29. *                   Export newPort
  30. newPort             start
  31. *                   Import killPort
  32.  
  33.                     DefineStack
  34.  
  35. keepPort            long                ;Must be at 1,s.
  36. portPtr             long                ;Pass NULL if mem needed for port.
  37. portHndl            long
  38. bitMapPtr           long                ;Pass NULL if mem needed for bitMap.
  39. bitMapHndl          long
  40. bitMapAttr          word                ;Memory attributes for bitMap.
  41. portRect            block 8
  42. bitMapRect          block 8
  43. endDesc             block 0             ;Descriptor locals end here.
  44.  
  45. portInfo            block oportRect-oportInfo
  46. bitMapHeight        word
  47. bmPtr               long                ;Pointer to bitMap, if we made one.
  48. tempPtr             long                ;Temporary bitMap work pointer.
  49.  
  50. sizeLocals          EndLocals
  51.  
  52. saveDPage           word
  53. returnAddr          block 3
  54.  
  55.                     BegParms
  56. portDescPtr         long                ;Pointer to port descriptor.
  57. sizeParms           EndParms
  58.  
  59. retval              long                ;We will return the new grafPortPtr.
  60.  
  61. ***
  62.  
  63.                     phd                 ;Save directPage register.
  64.                     tsc                 ;Make space for locals.
  65.                     sec
  66.                     sbc #sizeLocals
  67.                     tcd                 ;Set directPage register.
  68.                     tcs
  69.  
  70.                     _GetPort            ;keepPort is result space.
  71.  
  72.                     ldy #endDesc-portPtr-2
  73. aa                  tyx                 ;Copy descriptor data into directPage.
  74.                     lda [portDescPtr],y
  75.                     sta portPtr,x
  76.                     dey
  77.                     dey                 
  78.                     bpl aa
  79.  
  80.                     stz portHndl        ;Flag that we don't have handles yet.
  81.                     stz portHndl+2
  82.                     stz bitMapHndl
  83.                     stz bitMapHndl+2
  84.  
  85.                     stz retval          ;Assume we fail.
  86.                     stz retval+2
  87.  
  88.                     lda portPtr+1       ;See if we were passed a port pointer.
  89.                     bne givenPortMem    ;We were.
  90.  
  91.                     pha                 ;Result space.
  92.                     pha
  93.                     pea portSize|-16    ;Size of handle.
  94.                     pea portSize
  95.                     pha
  96.                     _MMStartUp          ;userID.
  97.                     pea attrLocked+attrFixed+attrNoCross
  98.                     lda #0              ;No specific address.
  99.                     pha
  100.                     pha
  101.                     _NewHandle
  102.                     plx
  103.                     ply
  104.                     bcc havePortHndl    ;We succeeded at creating a port handle.
  105.                     brl exit            ;Failed because we couldn't get memory.
  106.  
  107. havePortHndl        stx portHndl        ;We succeeded, so save the handle.
  108.                     sty portHndl+2
  109.  
  110.                     ldy #2              ;Dereference the handle to a grafPortPtr.
  111.                     lda [portHndl]
  112.                     sta portPtr
  113.                     lda [portHndl],y
  114.                     sta portPtr+2
  115.  
  116. givenPortMem        pei portPtr+2       ;Now that we have grafPort mem, in either
  117.                     pei portPtr         ;case, try opening the port.
  118.                     _OpenPort
  119.                     bcc portOpened      ;Hey, it worked!
  120.  
  121.                     tay                 ;Keep error code.
  122.                     lda #$8000          ;This flag indicates that the _OpenPort
  123.                     tsb portHndl+2      ;failed.  If it failed, we should not
  124. *                                       ;do a _ClosePort.
  125.                     tya                 ;Restore error code.
  126.                     brl exit            ;Failed because we couldn't get memory.
  127.  
  128. portOpened          pha                 ;Push parms to make visRgn rectangular.
  129.                     pha
  130.                     _GetVisHandle
  131.                     pea 0
  132.                     tdc
  133.                     clc
  134.                     adc #portRect
  135.                     pha
  136.  
  137.                     pha                 ;Push parms to make clipRgn rectangular.
  138.                     pha
  139.                     pea 0
  140.                     pha
  141.  
  142.                     _RectRgn
  143.                     _RectRgn
  144.  
  145.                     pea 0               ;Get the new port's locInfo structure.
  146.                     tdc
  147.                     clc
  148.                     adc #portInfo
  149.                     pha
  150.                     _GetPortLoc
  151.  
  152.                     lda bitMapRect+oh2  ;Get width of bitMap rectangle.
  153.                     sec
  154.                     sbc bitMapRect+oh1
  155.                     ldx portInfo+oportSCB-1
  156. *                                       ;Check bit 7 of the portSCB.
  157.                     bmi ab              ;The -1 is so we can bmi test.
  158.                     asl a               ;Pretend it is 640 for below math.
  159.  
  160. ab                  clc                 ;Convert 640 width into rowBytes.
  161.                     adc #3
  162.                     lsr a
  163.                     lsr a
  164.                     clc
  165.                     adc #7
  166.                     and #$FFF8
  167.                     sta portInfo+owidth
  168.  
  169.                     ldx bitMapPtr+1     ;See if we need to make a bitMap.
  170.                     bne givenBitMapMem  ;We don't need to.
  171.  
  172.                     pha                 ;Result space for _NewHandle
  173.                     pha
  174.                     pha                 ;Result space for _Multiply
  175.                     pha
  176.                     pha                 ;_Multiply param 1 (acc is still width).
  177.                     lda bitMapRect+ov2
  178.                     sec
  179.                     sbc bitMapRect+ov1
  180.                     sta bitMapHeight
  181.                     pha                 ;_Multiply param 2
  182.                     _Multiply
  183.                     pha
  184.                     _MMStartUp          ;userID.
  185.                     pei bitMapAttr      ;Memory attributes for bitMap.
  186.                     lda #0              ;No special memory location.
  187.                     pha
  188.                     pha
  189.                     _NewHandle
  190.                     plx
  191.                     ply
  192.                     bcs exit            ;Failed because we couldn't get memory.
  193.                     stx bitMapHndl
  194.                     sty bitMapHndl+2
  195.  
  196.                     ldy #2              ;Dereference bit Map handle.
  197.                     lda [bitMapHndl]
  198.                     sta bmPtr
  199.                     sta tempPtr
  200.                     lda [bitMapHndl],y
  201.                     sta bmPtr+2
  202.                     sta tempPtr+2
  203.  
  204.                     ldx bitMapHeight    ;Fill bitMap with white.
  205. white1              lda #$FFFF
  206.                     ldy portInfo+owidth
  207. white2              dey
  208.                     dey
  209.                     sta [tempPtr],y
  210.                     bne white2
  211.                     lda tempPtr
  212.                     clc
  213.                     adc portInfo+owidth
  214.                     sta tempPtr
  215.                     bcc ok
  216.                     inc tempPtr+2
  217. ok                  dex
  218.                     bne white1
  219.  
  220. givenBitMapMem      ldx #6              ;Copy the bounds rect into locInfo.
  221. moveBounds          lda bitMapRect,x
  222.                     sta portInfo+oboundsRect,x
  223.                     dex
  224.                     dex
  225.                     bpl moveBounds
  226.  
  227.                     lda bitMapPtr+2
  228.                     bmi defaultBitMap   ;Use QuickDraw default bitMap pointer.
  229.  
  230.                     ldx bitMapPtr
  231.                     ldy bitMapPtr+2
  232.                     lda bitMapPtr+1
  233.                     bne passedBitMap    ;Use bitMap pointer declared by application.
  234.  
  235.                     ldx bmPtr           ;Use pointer to bitMap we made.
  236.                     ldy bmPtr+2
  237.  
  238. passedBitMap        stx portInfo+optrToPixImage
  239.                     sty portInfo+optrToPixImage+2
  240.  
  241. defaultBitMap       pea 0               ;Set the portLoc, now that we have
  242.                     tdc                 ;finished setting it up.
  243.                     clc
  244.                     adc #portInfo
  245.                     pha
  246.                     _SetPortLoc
  247.  
  248.                     pea 0               ;Set the portRect to the passed rect.
  249.                     tdc
  250.                     clc
  251.                     adc #portRect
  252.                     pha
  253.                     _SetPortRect
  254.  
  255.                     lda portPtr         ;Everything worked, so pass back the
  256.                     sta retval          ;new grafPort pointer, instead of NULL.
  257.                     lda portPtr+2
  258.                     sta retval+2
  259.  
  260.                     clc                 ;We had no error.
  261.  
  262. exit                ldy #0              ;Assume no error.
  263.                     bcc noError         ;Hey, we were right!
  264.  
  265. error               pha                 ;Keep error code.
  266.                     pei portDescPtr+2
  267.                     pei portDescPtr
  268.                     jsl killPort        ;Dispose everything that was created.
  269.                     ply                 ;Restore error code.
  270.  
  271. noError             phy                 ;Keep error code (if any).
  272.  
  273.                     pei keepPort+2      ;Restore the port.
  274.                     pei keepPort
  275.                     _SetPort
  276.  
  277.                     ldy #endDesc-portPtr-2
  278. ac                  tyx                 ;Update the port descriptor.
  279.                     lda portPtr,x
  280.                     sta [portDescPtr],y
  281.                     dey
  282.                     dey
  283.                     bpl ac
  284.  
  285.                     ply                 ;Restore error code (if any).
  286.  
  287.                     tsc                 ;Get rid of local variables.
  288.                     clc
  289.                     adc #sizeLocals
  290.                     tcs
  291.                     pld                 ;Restore directPage register.
  292.                     lda 1,s             ;Move return address.
  293.                     sta <1+sizeParms,s
  294.                     lda 2,s
  295.                     sta <2+sizeParms,s
  296.                     tsc                 ;Get rid of passed parameters.
  297.                     adc #sizeParms
  298.                     tcs
  299.                     tya
  300.                     cmp #1              ;Set error status correctly.
  301.                     rtl
  302.  
  303.                     end
  304.  
  305. ********************
  306.  
  307. *                   Export killPort
  308. killPort            start
  309.  
  310.                     DefineStack
  311.  
  312. portPtr             long                ;Pass NULL if mem needed for port.
  313. portHndl            long
  314. bitMapPtr           long                ;Pass NULL if mem needed for bitMap.
  315. bitMapHndl          long
  316. endDesc             block 0             ;Descriptor locals end here.
  317. *                                       ;We don't need the entire descriptor.
  318.  
  319. sizeLocals          EndLocals
  320.  
  321. saveDPage           word
  322. returnAddr          block 3
  323.  
  324.                     BegParms
  325. portDescPtr         long                ;Pointer to port descriptor.
  326. sizeParms2          EndParms
  327.  
  328. ***
  329.  
  330.                     phd                 ;Save directPage register.
  331.                     tsc                 ;Make space for locals.
  332.                     sec
  333.                     sbc #sizeLocals
  334.                     tcd                 ;Set directPage register.
  335.                     tcs                 ;Allow us to use 0 as directPage address.
  336.  
  337.                     ldy #endDesc-portPtr-2
  338. ba                  tyx                 ;Copy descriptor data into directPage.
  339.                     lda [portDescPtr],y
  340.                     sta portPtr,x
  341.                     dey
  342.                     dey                 
  343.                     bpl ba
  344.  
  345.                     lda bitMapHndl+1
  346.                     beq notOurBitMap
  347.                     pei bitMapHndl+2
  348.                     pei bitMapHndl
  349.                     _DisposeHandle
  350.  
  351. notOurBitMap        lda portHndl+2
  352.                     bmi openPortFailed  ;This is our flag that the _OpenPort
  353. *                                       ;in newPort failed.  If it failed, then
  354. *                                       ;we don't want to do a _ClosePort on it.
  355.  
  356.                     pei portPtr+2       ;Close the port, since it opened okay.
  357.                     pei portPtr
  358.                     _ClosePort
  359.  
  360. openPortFailed      lda portHndl+1
  361.                     beq notOurPortMem
  362.                     lda portHndl+2
  363.                     and #$7FFF
  364.                     pha
  365.                     pei portHndl
  366.                     _DisposeHandle
  367.                     ldy #2              ;If we created a handle for the grafPort
  368.                     lda #0              ;memory, we did this because the portPtr
  369.                     sta [portDescPtr]   ;field was NULL.  If this is the case, we
  370.                     sta [portDescPtr],y ;need to return it to it's original value.
  371.                     
  372. notOurPortMem       tsc                 ;Get rid of local variables.
  373.                     clc
  374.                     adc #sizeLocals
  375.                     tcs
  376.                     pld                 ;Restore directPage register.
  377.  
  378.                     lda 1,s             ;Move return address.
  379.                     sta <1+sizeParms2,s
  380.                     lda 2,s
  381.                     sta <2+sizeParms2,s
  382.                     tsc                 ;Get rid of passed parameters.
  383.                     adc #sizeParms2
  384.                     tcs
  385.  
  386.                     lda #0              ;return no error.
  387.                     clc
  388.                     rtl
  389.  
  390.                     end
  391.  
  392. ********************
  393.  
  394.                     END
  395.